use time::{OffsetDateTime, format_description};
use time::formatting::Formattable;
use tracing::error;
use std::sync::atomic::AtomicU16;
use std::sync::LazyLock;

use crate::utils::current_data_time_with_zone;

//程序的全局变量，先放在这里备用的

///注册需要的事件,只有这里注册了的事件,才会被程序获取
// "event plain CHANNEL_CALLSTATE CHANNEL_STATE CHANNEL_CREATE CHANNEL_EXECUTE CHANNEL_EXECUTE_COMPLETE CHANNEL_ANSWER DTMF CHANNEL_BRIDGE CHANNEL_UNBRIDGE CHANNEL_PARK CHANNEL_UNPARK CHANNEL_HANGUP CHANNEL_HANGUP_COMPLETE CHANNEL_DESTROY API BACKGROUND_JOB CHANNEL_PROGRESS CHANNEL_APPLICATION CHANNEL_HOLD CHANNEL_UNHOLD CHANNEL_ORIGINATE CHANNEL_UUID SHUTDOWN HEARTBEAT PLAYBACK_STOP";
pub const ESL_EVENTS: &str = 
	"event plain CHANNEL_CALLSTATE CHANNEL_CREATE CHANNEL_ANSWER DTMF CHANNEL_BRIDGE CHANNEL_UNBRIDGE CHANNEL_HANGUP CHANNEL_APPLICATION CHANNEL_HOLD SHUTDOWN HEARTBEAT PLAYBACK_STOP BACKGROUND_JOB";
// pub const DATE_TIME_FORMAT: &str =  "%Y-%m-%d %H:%M:%S";

pub static DEFAULT_DATA_FORMAT: LazyLock<Vec<format_description::FormatItem<'_>>> = LazyLock::new(||{
		format_description::parse("[year]-[month]-[day]").expect("parse DEFAULT_DATA_FORMAT date format error")
	});

pub static DEFAULT_DATATIME_FORMAT: LazyLock<Vec<format_description::FormatItem<'_>>> = LazyLock::new(||{
	format_description::parse("[year]-[month]-[day] [hour]:[minute]:[second]").unwrap()
});

pub static EMPTY_STR :LazyLock<String> = LazyLock::new (||{
	String::default()
});

///直接格式化当前时间，使用默认的格式
#[inline]
pub fn format_current_dt_by_default() -> String {
	let dt = current_data_time_with_zone();

	format_date_time(&dt, &DEFAULT_DATA_FORMAT)
}


///格式化字符串
#[inline]
pub fn format_date_time(dt:&OffsetDateTime,format: &(impl Formattable + ?Sized)) -> String {
	match dt.format(format){
    Ok(s) => s,
    Err(e) => {
			error!("格式化日期出现异常。格式：{}",e);

			OffsetDateTime::now_utc().format(&DEFAULT_DATA_FORMAT).unwrap_or("".to_owned())
		},
	}
}


pub static CALLOUT_ID: AtomicU16 = AtomicU16::new(0);


